<?php
class ProductsController extends AppController {

	var $name = 'Products';
    var $components = array('Upload','Session');
    var $helpers = array('Html','Form','Session','Js','Javascript','Ajax');

	function index() {
		$this->Product->recursive = 0;
		$this->set('products', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid product', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('product', $this->Product->read(null, $id));
	}

	function add() {
		
        // get a list catalogs to load them into Add view
        $this->loadModel('Catalog');
        $this->set('catalog', $this->Catalog->find('list', array('fields'=> array('Catalog.name'))));
        $name = $this->data['Product']['name'];
        $price = $this->data['Product']['price'];
        $imageFile = $this->data['Product']['file_data'];
        $error = null;
        
        if(empty($this->data)){
            $this->render();
		}else{
            if($name==null || strlen($name)<=10)    $error = 'Vui lòng nhập \'Tên sản phẩm\' hợp lệ.';
            if($price==null)   $error = 'Vui lòng nhập \'Giá sản phẩm\' hợp lệ.';
            if($imageFile['error']==4)  $error = 'Vui lòng chọn hình ảnh cho sản phẩm.';
            if($imageFile['error']==1)  $error = 'Tệp hình ảnh bị lỗi hoặc sai định dạng.';
            
            if($error!=null)
                $this->Session->setFlash(__($error, true));
            
            if($error==null){
    			// set the upload destination folder
    			$destination = realpath('../../yourstyle/webroot/img/uploads/').'/';
    			// upload the image using the upload component
    			$result = $this->Upload->upload($imageFile, $destination, null, array('type' => 'resizecrop', 'size' => array('400', '300'), 'output' => 'jpg'));
    			if (!$result){
   				 $this->data['Product']['image'] = $this->Upload->result;
    			} else {
    				// display error
    				$errors = $this->Upload->errors;
    				// piece together errors
    				if(is_array($errors)){ $errors = implode("<br />",$errors); }
    				$this->Session->setFlash($errors);
    				$this->redirect('/images/upload');
    				exit();
    			}   
                if ($this->Product->save($this->data)) {
    				$this->Session->setFlash(__('The product has been saved', true));
    				//$this->redirect(array('action' => 'index'));
    			} else {
    				$this->Session->setFlash(__('The product could not be saved. Please, try again.', true));
                    unlink($destination.$this->Upload->result);
    			}
            }
		}
        
		$catalogs = $this->Product->Catalog->find('list');
		$this->set(compact('catalogs'));
	}

	function edit($id = null) {
        $name = $this->data['Product']['name'];
        $price = $this->data['Product']['price'];
        $imageFile = $this->data['Product']['file_data'];
        $error = null;
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid product', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
            if($name==null || strlen($name)<=10)    $error = 'Vui lòng nhập \'Tên sản phẩm\' hợp lệ.';
            if($price==null)   $error = 'Vui lòng nhập \'Giá sản phẩm\' hợp lệ.';
            if($imageFile==null)  $error = 'Vui lòng chọn hình ảnh cho sản phẩm.';
            
            if($error!=null)
                $this->Session->setFlash(__($error, true));
                
            if($error==null){
                echo "Testing id0:".$id;
    			// set the upload destination folder
    			$destination = realpath('../../yourstyle/webroot/img/uploads/').'/';
    			// upload the image using the upload component
    			$result = $this->Upload->upload($imageFile, $destination, null, array('type' => 'resizecrop', 'size' => array('400', '300'), 'output' => 'jpg'));
                echo "Testing id1:".$id;
    			if (!$result){
                    $this->data['Product']['image'] = $this->Upload->result;
                    echo "Testing id2:".$id;
    			} else {
    				// display error
    				$errors = $this->Upload->errors;
    				// piece together errors
    				if(is_array($errors)){ $errors = implode("<br />",$errors); }
    				$this->Session->setFlash($errors);
    				$this->redirect('/images/upload');
    				exit();
    			}
               	if ($this->Product->save($this->data)) {
    				$this->Session->setFlash(__('The product has been saved', true));
    				$this->redirect(array('action' => 'index'));
    			} else {
    				$this->Session->setFlash(__('The product could not be saved. Please, try again.', true));
                    unlink($destination.$this->Upload->result);
    			}
            }
          
		}
		if (empty($this->data)) {
			$this->data = $this->Product->read(null, $id);
		}
		$catalogs = $this->Product->Catalog->find('list');
		$this->set(compact('catalogs'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for product', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Product->delete($id)) {
			$this->Session->setFlash(__('Product deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Product was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
    
}
